//
//  UITableView-model.swift
//  plane
//
//  Created by tong on 16/3/28.
//  Copyright © 2016年 tong. All rights reserved.
//

import Foundation
import UIKit
import Eelay

extension UITableView {
    private struct AssociatedKeys {
        static var model = "model_key"
        static var cell_selector = "cell_selectors_key"
    }
    
    func checkHaveNibForCellName(cellName:String) ->Bool
    {
        
        var haveFile = false
        //
        if cellName.hasSuffix(".xib")
        {
            haveFile = true
            let nibNames = cellName.components(separatedBy: ".")
            let cellNibName = nibNames[0] as String
            
            
            let cellNib = UINib(nibName: cellNibName, bundle: nil)
            self.register(cellNib, forCellReuseIdentifier: cellName)
            
        }
        
        return haveFile;
        
    }
    
    
    var cell_selector: TP.selector? {
        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.cell_selector) as? [String:AnyObject]
        }
        set {
            if let fnewValue = newValue {
                
                objc_setAssociatedObject(self, &AssociatedKeys.cell_selector, fnewValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
                self.register(selector:fnewValue)
            }
        }
    }
    
}



extension UICollectionView
{
    private struct ColAssociatedKeys {
        static var model = "UICollectionViewmodel_key"
        static var style = "UICollectionViewstyle_key"
    }
    
    
    public var model: NSMutableDictionary {
        get {
            if let obj = objc_getAssociatedObject(self, &ColAssociatedKeys.model) as? NSMutableDictionary
            {
                return obj
            }
            let obj = NSMutableDictionary()
            self.model = obj
            return obj
            
        }
        set {
            objc_setAssociatedObject(self, &ColAssociatedKeys.model, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
}


extension UITableView
{
    private struct TabAssociatedKeys {
        static var model = "UITableViewmodel_key"
        static var style = "UITableViewstyle_key"
    }


    public var model: NSMutableDictionary {
        get {
            if let obj = objc_getAssociatedObject(self, &TabAssociatedKeys.model) as? NSMutableDictionary
            {
                return obj
            }
            let obj = NSMutableDictionary()
            self.model = obj
            return obj

        }
        set {


            objc_setAssociatedObject(self, &TabAssociatedKeys.model, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }

}


